package liuliu.throughwaring.ui.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.AlarmModel;
import liuliu.throughwaring.model.PersonModel;

/**
 * 报警列表页面
 * 作者：柳伟杰 on 2016/3/28 16:43
 * 邮箱：1031066280@qq.com
 */
public class WaringActivity extends BaseActivity {
    public static WaringActivity mIntails;
    @CodeNote(id = R.id.waring_toolbar)
    TToolbar waring_toolbar;
    @CodeNote(id = R.id.no_data_ll_waring, click = "onClick")
    LinearLayout no_data_ll_waring;
    @CodeNote(id = R.id.waring_lv)
    ListView waring_lv;
    CommonAdapter<AlarmModel> mAdapter;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_waring);
        mIntails = this;
        waring_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mIntails.finish();
            }
        });
    }

    @Override
    public void initEvents() {
        initData();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.no_data_ll_waring://重新加载数据
                initData();
                break;
        }
    }

    /**
     * 加载预警列表数据
     */
    private void initData() {
        List<AlarmModel> list = finalDb.findAllByWhere(AlarmModel.class, "1=1 order by createTime desc");
        if (list.size() > 0) {
            no_data_ll_waring.setVisibility(View.GONE);
            waring_lv.setVisibility(View.VISIBLE);
            mAdapter = new CommonAdapter<AlarmModel>(this, list, R.layout.item_waring) {
                @Override
                public void convert(ViewHolder holder, AlarmModel alarmModel, int position) {
                    List<PersonModel> personModels = finalDb.findAllByWhere(PersonModel.class, "PersonId='" + alarmModel.getPersonId() + "'");
                    if (personModels.size() > 0) {
                        holder.setText(R.id.user_name_tv_waring, personModels.get(position).getPersonName());
                        holder.setText(R.id.user_unit_tv_waring, personModels.get(position).getPersonAddress());
                    }
                    if (alarmModel.getHandleTime() != null) {
                        holder.setText(R.id.deal_result_tv_waring, "已处理");
                    } else {
                        holder.setText(R.id.deal_result_tv_waring, "待处理");
                    }
                    Utils.loadTime(alarmModel.getCreateTime());
                }
            };
            waring_lv.setAdapter(mAdapter);
        } else {
            no_data_ll_waring.setVisibility(View.VISIBLE);
            waring_lv.setVisibility(View.GONE);
        }
    }
}
